//
// Created by Kame.Liang on 2021/9/7.
//

#ifndef UNTITLED1_REFLECTFACTORY_H
#define UNTITLED1_REFLECTFACTORY_H
#include "../LazySingleton.h"
#include <string>
#include <map>

typedef void* (*createFunc)(void);
class CReflectFactory : public LazySingleton<CReflectFactory> {
public:
    CReflectFactory(){}

    void registeClass(std::string classname,createFunc func);

    void* getClassByName(std::string classname);
private:
    std::map<std::string,createFunc> classMap_;
};
#define reflectFactory CReflectFactory::GetInstance()

class CDynamicClass{
public:
    CDynamicClass(std::string name,createFunc func){
        reflectFactory->registeClass(name,func);
    }
};

#define DECLARE_CLASS(className) \
    private: \
        static CDynamicClass* m_##className##_dc; \
    public: \
        static void* createClassObj(){return new (std::nothrow)className;};
#define IMPLEMENT_CLASS(className)  \
    CDynamicClass* className::m_##className##_dc = \
        new CDynamicClass(#className, &className::createClassObj) ;

#endif //UNTITLED1_REFLECTFACTORY_H
